import requests
from urllib.parse import urlsplit
import html


class Articles():
    """
    文章信息
    """
    def __init__(self):
        self.headers = {
            'User-Agent':'Mozilla/5.0 (Linux; Android 9; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.62 XWEB/2691 MMWEBSDK/200901 Mobile Safari/537.36 MMWEBID/244 MicroMessenger/7.0.19.1760(0x27001339) Process/toolsmp WeChat/arm64 NetType/WIFI Language/zh_CN ABI/arm64',
            'Cookies': cookie,
            # 'referer': 'https://mp.weixin.qq.com/s?__biz=MzI0NTQ1NDc4Nw==&mid=2247486096&idx=1&sn=ff05026d4344867825c05d25afc312e4&chksm=e94f083cde38812a0c6115fe075700e2e5d42bb233dbcdda0b5f1c832a21b1408b51b4bde0a1&xtrack=1&scene=0&subscene=91&sessionid=1601821476&clicktime=1601821483&enterid=1601821483&ascene=7&devicetype=android-28&version=27001339&nettype=WIFI&abtest_cookie=AAACAA%3D%3D&lang=zh_CN&exportkey=AQYvulw6Wj5fAy5RWI2D4hY%3D&pass_ticket=wV1pEY2CRTLP9xeyYe3W6tj8Enlfa7a9sspazxpgJm24rB99j2gU9xAyGBDeH35F&wx_header=1&forceh5=1'
        }

        self.data = {
            'is_only_read': '1',
            'is_temp_url': '0',
            'appmsg_type': '9',
            # 'mid':'2247486096',
            # 'idx':'1',
            # 'sn':'ff05026d4344867825c05d25afc312e4',
            # 'abtest_cookie': 'AAACAA==',
            # 'comment_id': '1540155479425925121',
            # 'req_id': '0117ZJ7n1skNAlY8PcLydENa',
            # 'album_id': '1296223588617486300',
            # 'pass_ticket': pass_ticket,
            # 'scene': '0',
            # 'title': '%E7%88%B1%E5%8F%AF%E5%8F%AF%E5%BE%AE%E5%8D%9A%E7%83%AD%E9%97%A8%E5%88%86%E4%BA%AB(10%E6%9C%883%E6%97%A5)',
            # 'ct': '1601768098',
            # 'abtest_cookie': 'AAACAA==',
            # 'devicetype': 'android-28',
            # 'version': '27001339',
            # 'comment_id': '1546453771352850432',
            # 'is_need_reward': '1',
            'reward_uin_count': '23',
            # 'msg_daily_idx': '1',
            # 'req_id': '050052Wak1PEJqhUtgUaGNSe',
            # 'tmp_version': '0',
            # 'appmsg_like_type': '2',
            # 'related_video_num': '4',
            # 'album_id': '1296223588617486300',
            # 'album_video_num': '5',
            # 'is_need_ticket': '0',
            # 'is_need_ad': '0',
            # 'both_ad': '0',
            # 'is_original': '0',
            # 'item_show_type': '0',
            # 'more_read_type': '0',
            # 'is_pay_subscribe': '0',
            # 'pay_subscribe_uin_count': '0',
            # 'has_red_packet_cover': '0'
        }
        self.params = {
            "__biz": biz,
            "mid": mid,
            "sn": sn,
            "idx": idx, 
            "pass_ticket": pass_ticket,
            "appmsg_token": appmsg_token,
            "key": '777',
            "uin": '777',
            "wxtoken": "777",
  

        }
       
    # 分离出需要的参数，转为字典格式
    # def str_to_dict(self, url):
    #     url_split = url.split('&') # 以 & 为分割点进行切分
    #     params_dic = {}
    #     for i in url_split:
    #         x = i.find('=')
    #         key = i[:x]
    #         value = i[x+1:]
    #         params_dic[key] = value

        
    #     print('params_dic', params_dic)
    #     return params_dic        

    # def get_params(self, article_url):
    #     """
    #     获取文章url的参数
    #     """
    #     # article_url = html.unescape(article_url) # 将字符串 s 中的所有命名和数字字符引用 (例如 &gt;, &#62;, &#x3e;) 转换为相应的Unicode字符。
    #     # print('article_url_unescape', article_url)
    #     url_params = self.str_to_dict(urlsplit(article_url).query)
    #     print('url_params', url_params)
    #     return url_params

    def get_appmsgext(self):
        """
        请求阅读数
        """
        
        appmsgext_url = 'https://mp.weixin.qq.com/mp/getappmsgext?'

        print('self.data', self.data)

        r = requests.post(appmsgext_url, data=self.data, params=self.params, headers=self.headers)
        print(r)
        print(r.text)
        appmsgext_json = r.json()
        print('appmsgext_json', appmsgext_json)

        # if "appmsgstat" not in appmsgext_json.text():
        #     raise Exception(appmsgext_json)
        return appmsgext_json

    def read_like_nums(self):
        """
        获取数据
        :read_num: 阅读
        :old_like_num: 赞
        :like_num: 在看
        """
        appmsgstat = self.get_appmsgext()['appmsgstat']
        return appmsgstat["read_num"], appmsgstat["old_like_num"], appmsgstat["like_num"]



if __name__ == "__main__":
    cookie = 'rewardsn=; wxtokenkey=777; wxuin=682666741; devicetype=android-28; version=27001339; lang=zh_CN; pass_ticket=wV1pEY2CRTLP9xeyYe3W6tj8Enlfa7a9sspazxpgJm24rB99j2gU9xAyGBDeH35F; cert=EcEQ7pMqfU1FXuWWDa3USWs5MX3X5Stf; wap_sid2=CPXVwsUCEooBeV9ISVAxMWpPRDFfbTQtMnpSWnlfODIxRzdOeUlCUGhWRG1PSHZYa3FFS0xTTWVfSThsek9PbkN5dEJHRC1tVGpyNEJPaVJZN1NubDRTR1BadUthQWNvN0JQOVBsQl9fRE1jQ1h0RGlMX0VsbUNLODhXRVVOLWxhVGNwZmZPTERRcUZSY1NBQUF+MJLo5/sFOA1AAQ=='
    article_url = 'https://mp.weixin.qq.com/mp/getappmsgext?f=json&mock=&uin=777&key=777&pass_ticket=wV1pEY2CRTLP9xeyYe3W6tj8Enlfa7a9sspazxpgJm24rB99j2gU9xAyGBDeH35F&wxtoken=777&devicetype=android-28&clientversion=27001339&__biz=MzI2MTMxNzg1OA%3D%3D&enterid=1601827844&appmsg_token=1081_w0mrfXpobGl%252Frqn3dpZfJ927bjB60J4nDVxwKR9gd5sHTdnvYE22gxSgTkUxnZIEG5KrBIo1iwqt21WK&x5=0&f=json'
    pass_ticket = 'wV1pEY2CRTLP9xeyYe3W6tj8Enlfa7a9sspazxpgJm24rB99j2gU9xAyGBDeH35F'
    appmsg_token = '1081_w0mrfXpobGl%252Frqn3dpZfJ927bjB60J4nDVxwKR9gd5sHTdnvYE22gxSgTkUxnZIEG5KrBIo1iwqt21WK&x5=0'
    biz = 'MzI2MTMxNzg1OA%3D%3D'
    mid = '2247486725'
    sn = '88aad4bc1ade12d5a6921fd8a043d265'
    idx = '2'

    info = Articles()
    a, b, c = info.read_like_nums()


# __biz， mid， idx，sn

# https://mp.weixin.qq.com/mp/getappmsgext?